<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Order extends AdminBackEndController {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('repo/order_model', 'order_model');
        $this->load->library('mypaging');
    }

    private function intialPaging()
    {
        $this->mypaging->setCount($this->order_model->countListShopSumary());
        $this->mypaging->setLimit(ORDER_LIMIT);
        $this->mypaging->setMainURL(admin_order_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex=1)
    {
        $this->intialPaging();
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $offset = ($pageIndex - 1) * ORDER_LIMIT;
        $aOrder = $this->order_model->listShopSumary(ORDER_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'Hóa đơn',
            'aOrder' => $aOrder,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/admin/order/index', $aReturn);
    }

    private function intialShopPaging($shop_slug)
    {
        $this->mypaging->setCount($this->order_model->countOrderPerShop($shop_slug));
        $this->mypaging->setLimit(ORDER_LIMIT);
        $this->mypaging->setMainURL(admin_shop_order_paging_url($shop_slug));
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function getShopOrder($shop_slug, $pageIndex=1){
        $this->intialShopPaging($shop_slug);
        $offset    = ($pageIndex - 1) * ORDER_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aOrder    = $this->order_model->paging($shop_slug, ORDER_LIMIT, $offset);
        $aReturn   = array(
            'pageTitle' => 'Hóa đơn',
            'aOrder'    => $aOrder,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/admin/order/single', $aReturn);
    }

    public function get($id){
        $aOrderDetail = $this->order_model->get($id, true);
        $shop_image_folder = $this->input->get('image_folder', true);
        $aReturn = array(
            'pageTitle' => 'Hoá đơn #' . $id,
            'image_folder' => $shop_image_folder,
            'aOrderDetail' => $aOrderDetail);
        $this->load->view('backend/admin/order/detail.html.php', $aReturn);
    }

    private function intialSearchPaging($search_key)
    {
        $this->mypaging->setCount($this->order_model->countSearch($search_key));
        $this->mypaging->setLimit(ORDER_LIMIT);
        $this->mypaging->setMainURL(admin_order_search_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function search($pageIndex=1){
        $search_key = $this->input->get('s', true);
        $this->intialSearchPaging($search_key);
        $pagingBar = $this->mypaging->getBar($pageIndex, array('s'=>$search_key));
        $offset = ($pageIndex - 1) * ORDER_LIMIT;
        $aOrder = $this->order_model->search($search_key, ORDER_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'Hóa đơn',
            'aOrder' => $aOrder,
            'searchkey' => $search_key,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/admin/order/index', $aReturn);
    }
}
?>